<?php
	require_once "admin_header.php";
	$data=new Database;
	$list_user=$muser->show_user();
	$list_level=$muser->show_level();
	
	if(isset($_POST['btthem']))
	{
		$level= $_POST['txtlevel'];
		$ten=$_POST['txttentk'];
		$email=$_POST['txtemail'];
		$pas=md5($_POST['txtpass']);
		if( strlen($ten)<6 || strlen($ten)>16 )
			$loi[]= "Tên phải từ 6-16 ký tự.";
		else if( strlen($pas)<6 || strlen($pas)>32 )
			$loi[]= "Mật khẩu phải từ 6-16 ký tự.";
		else
		{
			$sql="insert into user(user_name,pass_word,email,level_id) values('".$ten."','".$pas."','".$email."','".$level."')";
			//echo $sql;
			$data->query($sql);
			echo "<script> alert('Bạn vừa thêm 1 tài khoản'); window.location='admin_phanquyen.php'</script>";
		}
	}
?>
<script type="text/javascript" language="javascript">
	page=5;
</script>
<div class="heading">
    <a>phân quyền</a>
</div>
<div class="content_page">
	
	<div class="ds">
    	<fieldset>
        	<legend> Thêm Tài Khoản</legend>
            <form method="post" enctype="multipart/form-data" name="fupdate">
                <table border="1">
                	<tr>
                    	<td>Tên tài khoản:</td>
                    	<td><input type="text" name="txttentk" size="25" required="required" /></td>
                    </tr>
                    <tr>
                    	<td>Cấp Bậc:</td>
                        <td>
                            <select name="txtlevel" id="txtlevel" style="height:auto; width:175px;"  >
                                <?php
                                   
                                    foreach($list_level as $k)
                                    {
                                        if($k["level_id"]==1)
                                            $lv= "Quản trị";
                                        else
                                            $lv= "Quản trị viên";
                                        
                                        if($k["level_id"]==$level_id)
                                            echo "<option value='$k[level_id]' selected='selected'>$lv</option>";
                                        else
                                            echo "<option value='$k[level_id]'>$lv</option>";
                                    }
                                    
                                ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                    	<td>Email:</td>
                        <td><input type="email" name="txtemail" size="25" required="required" /></td>
                    </tr>
                    <tr>
                    	<td>Mật Khẩu:</td>
                        <td><input type="text" name="txtpass" size="25" required="required" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align:center">
                            <input type="submit" name="btthem" value="Thêm Tài Khoản">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                        	<?php
								if(isset($loi))
								{
									foreach($loi as $item)
										echo "<span style='color:red'>".$item ."</span></br>";
								}
							?>
                        </td>
                    </tr>
                </table>
              </form>
        </fieldset>
    	<fieldset>
		<legend>Danh Sách Tài Khoản</legend>
        <table border="1">
            <tr>
                <th>STT</th>
                <th>Tên Tài khoản</th>
                <th>Email</th>
                <th> Cấp</th>
                <th> Sửa</th>
                <th> Xóa</th>
            </tr>
            <?php
                
                $stt=0;
                
                foreach($list_user as $k)
                {
                    if($k["level_id"]==1)
                        $lv="<td class='list_pq' style='font-weight:bold;color:red'>Quản trị</td>";
                    else
                        $lv="<td class='list_pq'>Quản trị viên</td>";
                    echo "<tr>";
                    $stt++;
                    echo "<td class='list_pq'>$stt</td><td>$k[user_name]</td><td>$k[email]</td>$lv<td class='list_pq'><a href='admin_suauser.php?user_id=$k[user_id]&level_id=$k[level_id]'>Sủa</a></td><td class='list_pq'><a href='libraries/xoa_phanquyen_user.php?id=$k[user_id]&lv=$k[level_id]'>Xóa</a></td>";
                    echo "</tr>";
                }
                
            ?>
        </table>
        </fieldset>
	</div>
</div>
<?php
	require_once "admin_footer.php";
?>